#include <jni.h>
#include <string>
#include <iostream>
#include <android/bitmap.h>

#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))


extern "C" JNIEXPORT jint JNICALL
Java_com_zpy_wallpaperhsb_ImageNativeTool_getBitmapBrightByArr(
        JNIEnv *env,
        jobject /* this */, jintArray buffer, jint jwidth, jint jheight) {

    jint* source = env->GetIntArrayElements(buffer, 0);
    int width = jwidth;
    int height = jheight;
    int r, g, b;
    int count = 0;
    int bright = 0;
    count = width * height;
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            int localTemp = source[j * width + i];
            r = (localTemp >> 16) & 0xff;
            g = (localTemp >> 8) & 0xff;
            b = localTemp & 0xff;
            bright = (int) (bright + 0.299 * r + 0.587 * g + 0.114 * b);
        }
    }
    env->ReleaseIntArrayElements(buffer, source, 0);
    return bright / count;

}

extern "C"
JNIEXPORT jfloat JNICALL
Java_com_zpy_wallpaperhsb_ImageNativeTool_getBitmapHueByArr(JNIEnv *env, jobject instance,
                                                            jintArray buffer, jint jwidth,
                                                            jint jheight) {
    jint* source = env->GetIntArrayElements(buffer, 0);
    int width = jwidth;
    int height = jheight;
    int r, g, b;
    int count = 0;
    float hue = 0;
    float hueSum = 0;
    count = width * height;
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            int localTemp = source[j * width + i];
            r = (localTemp >> 16) & 0xff;
            g = (localTemp >> 8) & 0xff;
            b = localTemp & 0xff;
            int maX = max(max(r,g),b);
            int miN = min(min(r,g),b);


            if (r == maX)
            {
                hue = (maX - miN) == 0? 0 : (g - b) / (float)(maX - miN);
            }
            if (g == maX)
            {
                hue = 2 + ((maX - miN) == 0? 0: (b - r) / (float)(maX - miN));
            }
            if (b == maX)
            {
                hue = 4 + ((maX - miN) == 0? 0:(r - g) / (float)(maX - miN));
            }
            hue = (hue / 6.0);
            if (hue < 0)
            {
                hue = (hue / 360.0 + 1);
            }

            hueSum += hue;

        }
    }
    env->ReleaseIntArrayElements(buffer, source, 0);
    return hueSum / (float) count;
}

extern "C"
JNIEXPORT jfloat JNICALL
Java_com_zpy_wallpaperhsb_ImageNativeTool_getBitmapSaturationByArr(JNIEnv *env, jobject instance,
                                                                   jintArray buffer, jint jwidth,
                                                                   jint jheight) {

    jint* source = env->GetIntArrayElements(buffer, 0);
    int width = jwidth;
    int height = jheight;
    int r, g, b;
    int count = 0;
    float saturation = 0;
    count = width * height;
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            int localTemp = source[j * width + i];
            r = (localTemp >> 16) & 0xff;
            g = (localTemp >> 8) & 0xff;
            b = localTemp & 0xff;
            int maX = max(max(r,g),b);
            int miN = min(min(r,g),b);


            saturation += maX == 0? 0 : (maX - miN) / (float)maX;
        }
    }
    env->ReleaseIntArrayElements(buffer, source, 0);
    return saturation / (float)count;

}

